using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using PropertyExpression.PresentationCode;
using System.Windows.Data;

namespace WpfApplication1
{
    public partial class Window2
    {
        private static readonly SolidColorBrush borderBrush = new SolidColorBrush().Color(Color.FromRgb(204, 204, 204));

        private static readonly Style fontStyle = new Style().Setters(
            new Setter().Control_FontSize(11),
            new Setter().Control_FontFamily("Tahoma"));

        public Window2()
        {
            InitializeComponent();

            this.SnapsToDevicePixels(true).Nested(
                new Border().Padding(5).Nested(
                    new Grid().RowDefinitions(
                        new RowDefinition().Height_Auto(),
                        new RowDefinition()).Nested(
                        new Grid().ColumnDefinitions(
                            new ColumnDefinition(),
                            new ColumnDefinition().Width(18),
                            new ColumnDefinition()).Nested(
                            Section("Customer",
                                    new Grid().RowDefinitions(
                                        new RowDefinition().Height_Auto(),
                                        new RowDefinition().Height_Auto(),
                                        new RowDefinition().Height_Auto(),
                                        new RowDefinition().Height_Auto(),
                                        new RowDefinition().Height_Auto(),
                                        new RowDefinition().Height_Auto(),
                                        new RowDefinition().Height_Auto()).ColumnDefinitions(
                                        new ColumnDefinition().Width_Auto(),
                                        new ColumnDefinition(),
                                        new ColumnDefinition().Width_Auto(),
                                        new ColumnDefinition()).Nested(
                                        TextBlock(0, 0).Text("Name:"), TextBox(0, 1).Grid_ColumnSpan(3),
                                        TextBlock(1, 0).Text("Contact Name:"), TextBox(1, 1).Grid_ColumnSpan(3),
                                        TextBlock(2, 0).Text("Phone:"), TextBox(2, 1),
                                        TextBlock(2, 2).Text("Fax:").HorizontalAlignment_Right(), TextBox(2, 3),
                                        TextBlock(3, 0).Text("Address:"), TextBox(3, 1).Grid_ColumnSpan(3),
                                        TextBlock(4, 0).Text("City:"), TextBox(4, 1),
                                        new Grid().Grid_Position(4, 2).Grid_ColumnSpan(2)
                                            .ColumnDefinitions(
                                            new ColumnDefinition().Width_Auto(),
                                            new ColumnDefinition()
                                            ).Nested(
                                            TextBlock(0, 0).Text("State/Province:"), TextBox(0, 1)),
                                        TextBlock(5, 0).Text("ZIP/Postal code:"), TextBox(5, 1),
                                        TextBlock(5, 2).Text("Country:"), TextBox(5, 3))).Grid_Position(0, 0),
                            Section("Dealer",
                                    new Grid().RowDefinitions(
                                        new RowDefinition().Height_Auto(),
                                        new RowDefinition().Height_Auto()).ColumnDefinitions(
                                        new ColumnDefinition().Width_Auto(),
                                        new ColumnDefinition()).Nested(
                                        TextBlock(0, 0).Text("First Contact Name:"), TextBox(0, 0 + 1),
                                        TextBlock(1, 0).Text("Second Contact Name:"), TextBox(1, 0 + 1)))
                                .Grid_Position(0, 2)
                            )
                        )
                    )
                );
        }

        private static Grid Section(string sectionName, UIElement nestedElement)
        {
            return new Grid().VerticalAlignment_Top().RowDefinitions(
                new RowDefinition().Height_Auto(),
                new RowDefinition()).Nested(
                new Border().Grid_Position(0, 0).BorderThickness(1).Padding(10, 5, 10, 4).BorderBrush(borderBrush)
                    .Background(new SolidColorBrush().Color(Color.FromRgb(128, 128, 128))).Nested(
                    new Grid().Nested(
                        new TextBlock().Text(sectionName).Style(fontStyle).FontWeight_Bold()
                            .FontWeight_Bold().Foreground_White())
                    ),
                new Border().Grid_Position(1, 0).BorderThickness(1, 0, 1, 1)
                    .BorderBrush(borderBrush).Padding(0, 11, 0, 11).Nested(nestedElement)
                );
        }

        private static TextBox TextBox(int row, int column)
        {
            return new TextBox().Grid_Position(row, column)
                .VerticalAlignment_Center().Margin(5, 5, 5, 5)
                .Style(fontStyle).Template(
                new ControlTemplate().Nested(
                    New<Border>().BorderThickness(1).Padding(2)
                        .BorderBrush(new SolidColorBrush().Color(Color.FromRgb(153, 153, 153)))
                        .Nested(New<ScrollViewer>().Name("PART_ContentHost")))
                );
        }

        private static TextBlock TextBlock(int row, int column)
        {
            return new TextBlock().Grid_Position(row, column).Margin(5, 0, 5, 0)
                .MaxWidth(75).TextWrapping_Wrap().Style(fontStyle)
                .VerticalAlignment_Center();
        }

        private static void Test()
        {
            new Grid().RowDefinitions(
                new RowDefinition().Height_Auto(),
                new RowDefinition().Height(1, GridUnitType.Star),
                new RowDefinition().Height_Auto()).Nested(
                new Expander().Header(
                    new ContentPresenter().Content(new Binding("Property1")).ContentTemplate(new Resource("template2"))).Nested(
                    new Grid().RowDefinitions(
                        new RowDefinition().Height_Auto(),
                        new RowDefinition().Height(1, GridUnitType.Star)).Nested(
                        new ContentPresenter().Content(new Binding()).ContentTemplate(new Resource("template3")),
                        new ContentPresenter().Grid_Row(1).Content(new Binding()).ContentTemplate(new Resource("template4"))
                        )
                    )
                );
        }

        protected static IPropertyProvider<T> C<T>()
        {
            return Property.C<T>();
        }

        protected static IElement<T> New<T>()
        {
            return Property.New<T>();
        }
    }
}